<?php

namespace ClassLibrary;

/**
 * RSA非对称加解密
 * Class ClRsa
 * @package ClassLibrary
 * @author SongKeJing qq:597481334 mobile:159-5107-8050
 * @date 2022/3/17 16:54
 */
class ClRsa {

    /**
     * 获取新的密钥
     * @param int $duration 缓存时长
     * @return array
     * @author SongKeJing qq:597481334 mobile:159-5107-8050
     * @date 2022/3/17 17:27
     */
    public static function getNewKey($duration = 3600) {
        $key = '';
        if ($duration > 0) {
            $key     = ClCache::getKey();
            $content = cache($key);
            if ($content !== false) {
                return $content;
            }
        }
        $config = __DIR__ . '/rsa/openssl.cnf';
        $res    = openssl_pkey_new([
            'digest_alg'       => 'sha512',
            //字节数  512 1024 2048  4096 等 ,不能加引号，此处长度与加密的字符串长度有关系，可以自己测试一下
            'private_key_bits' => 512,
            //加密类型
            'private_key_type' => OPENSSL_KEYTYPE_RSA,
            //修改默认配置文件地址
            'config'           => $config,
        ]);
        //提取私钥
        openssl_pkey_export($res, $private_key, null, ['config' => $config]);
        //生成公钥
        $public_key = openssl_pkey_get_details($res);
        $content    = [
            'private'     => $private_key,
            'public'      => $public_key['key'],
            'create_time' => time(),
        ];
        if ($duration > 0) {
            //写入缓存
            cache($key, $content, $duration);
        }
        return $content;
    }

    /**
     * 私钥加密
     * @param $content
     * @param $private_key
     * @return string
     * @author SongKeJing qq:597481334 mobile:159-5107-8050
     * @date 2022/3/17 17:39
     */
    public static function encodeWithPrivateKey($content, $private_key) {
        //加密
        openssl_private_encrypt($content, $encrypted, $private_key);
        //加密后的内容通常含有特殊字符，需要base64编码转换下
        return base64_encode($encrypted);
    }

    /**
     * 公钥加密
     * @param $content
     * @param $public_key
     * @return mixed
     * @author SongKeJing qq:597481334 mobile:159-5107-8050
     * @date 2022/3/17 17:39
     */
    public static function encodeWithPublicKey($content, $public_key) {
        openssl_public_encrypt($content, $encrypted, $public_key);
        return base64_encode($encrypted);
    }

    /**
     * 私钥解密
     * @param $encode_content
     * @param $private_key
     * @return mixed
     * @author SongKeJing qq:597481334 mobile:159-5107-8050
     * @date 2022/3/17 17:42
     */
    public static function decodeWithPrivateKey($encode_content, $private_key) {
        openssl_private_decrypt(base64_decode($encode_content), $decrypted, $private_key);
        return $decrypted;
    }

    /**
     * 公钥解密
     * @param $encode_content
     * @param $public_key
     * @return mixed
     * @author SongKeJing qq:597481334 mobile:159-5107-8050
     * @date 2022/3/17 17:41
     */
    public static function decodeWithPublicKey($encode_content, $public_key) {
        openssl_public_decrypt(base64_decode($encode_content), $decrypted, $public_key);
        return $decrypted;
    }

}